using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Ninject;
using Soundteller.Application.Components;
using Soundteller.Application.Facade;

namespace Soundteller.Application
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game : Microsoft.Xna.Framework.Game
    {
        private GraphicsDeviceManager _graphicsDeviceManager;
        private SpriteBatch _spriteBatch;

        #region Components

        private AudioComponent _audioComponent;
        private SoundtellerComponent _SoundtellerComponent;

        [Inject]
        public AudioComponent AudioComponent
        {
            set 
            { 
                _audioComponent = value;
                _audioComponent.UpdateOrder = 0;

                if(!Components.Contains(_audioComponent))
                    Components.Add(_audioComponent);
            }
        }

        [Inject]
        public SoundtellerComponent SoundtellerComponent
        {
            set
            {
                _SoundtellerComponent = value;
                _SoundtellerComponent.UpdateOrder = 1;
                _SoundtellerComponent.DrawOrder = 1;

                if(!Components.Contains(_SoundtellerComponent))
                    Components.Add(_SoundtellerComponent);
            }
        }

        #endregion

        public Game()
        {
            _graphicsDeviceManager = new GraphicsDeviceManager(this);
            _graphicsDeviceManager.DeviceCreated += ((sender, e) => GraphicsContext.GraphicsDevice = _graphicsDeviceManager.GraphicsDevice);

            Content.RootDirectory = "Content";
            GraphicsContext.Content = Content;
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            base.Initialize();

            Window.Title = "Soundteller";
            IsFixedTimeStep = false;
            IsMouseVisible = true;
            Window.AllowUserResizing = true;
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            _spriteBatch = new SpriteBatch(GraphicsDevice);

            // TODO: use this.Content to load your game content here
            base.LoadContent();
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here

            base.UnloadContent();
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.White);

            base.Draw(gameTime);
        }
    }
}